package com.telnetnz.ticketdesk.action.acp.project;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.rockbb.common.sso.session.SessionConstants;
import com.telnetnz.ticketdesk.action.base.BasicSessionPageAcpAction;
import com.telnetnz.ticketdesk.action.common.CommonUtils;
import com.telnetnz.ticketdesk.bean.Project;
import com.telnetnz.ticketdesk.bean.ProjectToUser;
import com.telnetnz.ticketdesk.service.ProjectService;
import com.telnetnz.ticketdesk.service.ProjectToUserService;
import com.telnetnz.ticketdesk.service.UserService;

public class AcpProjectListAction extends BasicSessionPageAcpAction
{
	private static Logger logger = Logger.getLogger(AcpProjectListAction.class);

	public String doList()
	{
		List<Project> projects = ProjectService.getByPageAndCondition(
				0, 
				100, 
				ProjectService.ORDERBY_NAME, 
				1, 
				null);

		this.put_page_key("projects", projects);
		return SUCCESS;
	}

	public String doView()
	{
		RequestBean bean = new DefaultRequestBean();
		int id = bean.getInt("id");
		Project project = ProjectService.getProject(id);
		if (project == null)
		{
			return this.message_box(
					langRes.get("global.error"),
					langRes.get("project.project-not-exist", new String[]{bean.getRedirect()}),
					bean.getRedirect(),
					2);
		}

		List<Long> excludedIds = new ArrayList<Long>();
		excludedIds.add(SessionConstants.USER_ID_ANONYMOUS);
		for (ProjectToUser user: project.getProjectUsers())
		{
			excludedIds.add(user.getUserId());
		}

		this.put_page_key("project", project);
		this.put_page_key("add_member_action", baseLink("acp/projectmemberadd"));
		this.put_page_key("user_options", CommonUtils.buildUserDropdown(
				UserService.getByPageAndCondition(0, 100, UserService.ORDER_BY_NAME, 1, excludedIds, null, null), 0L, false));
		this.put_page_key("role_options", CommonUtils.buildSingleDropdown(langRes, "project.role.", ProjectToUserService.ROLES, 0, false));
		return SUCCESS;
	}
}
